package dingyunchi.job.service.impl;

import dingyunchi.job.dao.StudentMapper;
import dingyunchi.job.entity.Student;
import dingyunchi.job.service.StudentService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    private final StudentMapper studentMapper;

    public StudentServiceImpl(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    @Override
    public Student save(Student student) {
        if (student.getAge() < 16 || student.getAge() > 30) {
            throw new IllegalArgumentException("年龄应在16到30之间");
        }
        studentMapper.save(student);
        return student;
    }

    @Override
    public Student findById(Long id) {
        return studentMapper.findById(id);
    }

    @Override
    public List<Student> findAll() {
        return studentMapper.findAll();
    }

    @Override
    public void update(Student student) {
        if (student.getAge() < 16 || student.getAge() > 30) {
            throw new IllegalArgumentException("年龄应在16到30之间");
        }
        studentMapper.update(student);
    }

    @Override
    public void delete(Long id) {
        studentMapper.delete(id);
    }
}